class Solution(object):
    def maxLevelSum(self, root):
        max_val = - float('inf')
        st = [root]
        dist = 1
        while st:
            now_val = 0
            next_st = []
            for node in st:
                now_val += node.val
                if node.left:
                    next_st.append(node.left)
                if node.right:
                    next_st.append(node.right)
            if now_val > max_val:
                max_val = now_val
                ans = dist
            st = next_st
            dist += 1
        return ans